package com.xiaohong.order.service.impl;


import com.xiaohong.goods.feign.SkuFeign;
import com.xiaohong.goods.feign.SpuFeign;
import com.xiaohong.goods.pojo.Sku;
import com.xiaohong.goods.pojo.Spu;
import com.xiaohong.order.pojo.OrderItem;
import com.xiaohong.order.service.CartService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 更新购物车
     * @param id
     * @param num
     */
    @Override
    public void update(String id, Integer num,String username) {
        // 先获取商品
         OrderItem o = (OrderItem) redisTemplate.boundHashOps("Cart_" + username).get(id);

        //如果添加的商品数量小于等于0，则清空购物车
        if (num<=0){
            // 判断商品数量减少后是否小于或等于0
            if (o.getNum()+num <= 0 ){
                //清空商品信息
                redisTemplate.boundHashOps("Cart_" + username).delete(id);
                return;
            }
            //否则在原有基础上递减
            o.setNum(o.getNum() + num);
            o.setMoney(o.getMoney()+o.getPrice()*num);
            o.setPayMoney(o.getPayMoney()+o.getPrice()*num);
            //放回去
            redisTemplate.boundHashOps("Cart_" + username).put(id,o);

            //再判断此时购物车是否为空
            Long size = redisTemplate.boundHashOps("Cart_" + username).size();
            if (size == 0){
                redisTemplate.delete("Cart_" + username);
            }
            return;
        }

        o.setNum(o.getNum()+num);
        o.setMoney(o.getMoney()+o.getPrice()*num);
        o.setPayMoney(o.getPayMoney()+o.getPrice()*num);

        //存储到redis中 id转为字符串是因为redis中文转码会报错
        redisTemplate.boundHashOps("Cart_"+username).put(id,o);
    }

    /**
     * 查询购物车
     * @param username
     * @return
     */
    @Override
    public List<OrderItem> list(String username) {
        return redisTemplate.boundHashOps("Cart_"+username).values();
    }

    /**
     * 首次添加购物车
     * @param id
     * @param username
     */
    @Override
    public void addFirst(String id, String username) {
        //查询商品的详细信息
        //查询sku
        Result<Sku> skuResult = skuFeign.findById(id);
        Sku sku = skuResult.getData();
        //查询spu
        Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());
        Spu spu = spuResult.getData();
        //封装成orderItem （第一次默认添加一件）
        OrderItem orderItem = CreatOrderItem(Long.valueOf(id), 1, sku, spu,username);
        //存储到redis中 id转为字符串是因为redis中文转码会报错
        redisTemplate.boundHashOps("Cart_"+username).put(id,orderItem);
    }

    /**
     * 根据id清理购物车
     * @param id
     * @param username
     */
    @Override
    public void deleteById(String id, String username) {
        redisTemplate.boundHashOps("Cart_" + username).delete(id);
    }

    private OrderItem CreatOrderItem(Long id, Integer num, Sku sku, Spu spu,String username) {
        OrderItem orderItem = new OrderItem();
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        orderItem.setId(id.toString());
        orderItem.setSpuId(spu.getId());
        orderItem.setImage(sku.getImages().split(",")[0]);
        orderItem.setName(sku.getName());
        orderItem.setNum(num);
        orderItem.setPrice(Float.valueOf(sku.getPrice()));
        orderItem.setMoney(Float.valueOf(num*sku.getPrice()));
        orderItem.setPayMoney(Float.valueOf(num*sku.getPrice()));
        orderItem.setPostFee(0F);
        orderItem.setUserId(username);
        orderItem.setSkuId(sku.getId());
        orderItem.setStatus(0);
        return orderItem;
    }
}
